<template>
  <el-dialog :visible.sync="dialogVisible" width="800px" :before-close="handleClose" :close-on-click-modal="false"
    append-to-body :title="title">
    <el-form :model="form" ref="formRef" label-width="140px"
      style="max-height: 580px; overflow: hidden;overflow-y: scroll;">

      <el-form-item label="团队名称" prop="team_name" :rules="[{ required: true, message: '请输入团队名称', trigger: 'blur' }]">
        <el-input placeholder="请输入" v-model="form.team_name"></el-input>
      </el-form-item>

      <el-form-item label="团队头像" prop="team_icon">
        <!--  {{ imgsDefalut }} -->
        <ImgUploadPath ref="ImgUploadPath" :limit="1" @success="onUploadSuccess" @remove="onUploadRemove"
          :imgsDefalut="imgsDefalut" />
      </el-form-item>
      <!-- 
      <el-form-item prop="sub_department_id" label="科室"
        :rules="[{ required: true, message: '请选择机构名称', trigger: 'blur' }]">
        <el-select v-model="form.sub_department_id" clearable filterable placeholder="请选择">
          <el-option v-for="item in subDepartMentArr" :key="item.name" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item> -->

      <el-form-item label="团队介绍" prop="team_intro" :rules="[{ required: true, message: '请输入团队介绍', trigger: 'blur' }]">
        <el-input placeholder="请输入" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }"
          v-model="form.team_intro"></el-input>
      </el-form-item>

      <el-form-item label="擅长领域" prop="team_field" :rules="[{ required: true, message: '擅长领域', trigger: 'blur' }]">
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请输入"
          v-model="form.team_field"></el-input>
      </el-form-item>

      <el-form-item label="接诊价格" prop="price" :rules="[
          { required: true, message: '请输入接诊价格', trigger: 'blur' },
        ]">
        <el-input placeholder="请输入" v-model="form.price"></el-input>
      </el-form-item>

      <el-form-item label="是否接诊" prop="open" :rules="[{ required: true, message: '请选择是否接诊', trigger: 'change' }]">
        <el-select v-model="form.open" clearable filterable placeholder="请选择">
          <el-option v-for="item in openArr" :key="item.name" :label="item.name" :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>

      <!--  doctorArr -->
      <el-form-item v-if="!this.id" label="团队医生" prop="members"
        :rules="[{ required: true, message: '请选择团队医生', trigger: 'blur' }]">
        <el-select v-model="form.members" clearable multiple filterable placeholder="请选择">
          <el-option v-for="item in doctorArr" :key="item.name" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="tx-c">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="confirm" :loading="btnLoading">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import { debounce } from "@/util";
import { ApiUpdateDoctorTeam, ApiAddDoctorTeam, ApigetDoctorList } from "@/api/index.js";
import ImgUploadPath from "@/components/imgUpload-path.vue";
export default {
  props: ["roleData", "departmentData", "unitList", "subDepartMentArr"],
  components: {
    ImgUploadPath,
  },
  data() {
    return {
      //职称
      //医生职称
      openArr: [
        { name: "接诊", code: 1 },
        { name: "不接诊", code: 0 },
      ],
      //护士职称

      oldform: {
        team_name: "",
        team_icon: "",
        team_intro: "",
        team_field: "",
        price: "",
        open: "",
        members: [],
      },
      form: {},
      btnLoading: false,
      dialogVisible: false,
      id: null,
      title: "",
      imgsDefalut: [],
      doctorArr: [],
      roleArr: [{ name: '医师', code: '医师' }, { name: '护师', code: '护师' }]
    };
  },
  computed: {
    ...mapState(["userState"]),
  },
  methods: {
    /*   getUnitList() {
        ApigetUnitList({ page: 1, size: 999 }).then(res => {
          this.unitListArr = res.data
        })
      }, */
    onUploadSuccess(path) {
      this.form.team_icon = path;
    },
    onUploadRemove() {
      this.form.pic = "";
    },
    confirm: debounce(
      function () {
        this.$refs["formRef"].validate(async (valid) => {
          if (valid) {
            this.btnLoading = true;

            try {
              if (this.id) {
                await ApiUpdateDoctorTeam(this.id, this.form);
                this.$message.success("修改成功");
              } else {
                await ApiAddDoctorTeam(this.form);
                this.$message.success("添加成功");
                /* this.$alert("添加成功，该员工初始密码为：123456", "提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {},
                }); */
              }

              this.handleClose();
              this.$emit("refresh");
            } finally {
              this.btnLoading = false;
            }
          } else return false;
        });
      },
      500,
      true
    ),

    show(row) {
      //获取全部医生列表
      this.getDocList()
      const data = row || this.oldform;
      for (const key in this.oldform) {
        this.$set(this.form, key, data[key]);
      }
      if (row) {
        this.id = row.id;
        this.title = "编辑医生团队";
        if (row.team_icon) this.imgsDefalut = [{ url: this.$path + row.team_icon }];
      } else {
        this.id = null;
        this.title = "添加医生团队";
      }
      this.dialogVisible = true;
    },

    getDocList() {
      ApigetDoctorList({ page: 1, size: 999, type: '医生' }).then(res => {
        this.doctorArr = res.data
      })
    },
    handleClose() {
      this.dialogVisible = false;
      this.form = { ...this.oldform };
      this.$nextTick((_) => {
        this.$refs.formRef.clearValidate();
        this.$refs.ImgUploadPath.clear();
      });
    },
  },
};
</script>

<style></style>
